﻿<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="mPassRT.Pages.GroupPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:common="using:mPassRT.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:converters="using:mPassRT.Converters"
    xmlns:selectors="using:mPassRT.Selectors"
    mc:Ignorable="d" FontFamily="Global User Interface" Background="{Binding DatabaseColor}">

    <Page.Resources>
        <x:Double x:Key="IconSize">110</x:Double>
        <Thickness x:Key="IconMargin">10</Thickness>
        
        <converters:FallbackConverter x:Key="FallbackConverter"/>

        <!-- Grid-appropriate 500 by 130 pixel item template as seen in the GroupDetailPage -->
        <DataTemplate x:Key="EntryTemplate">
            <Grid Height="110" Width="480" Margin="{StaticResource IconMargin}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="{StaticResource IconSize}" Height="{StaticResource IconSize}">
                    <TextBlock Style="{StaticResource CommonIconStyle}" Text="&#xE192;"
                               HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
                <StackPanel Grid.Column="1" VerticalAlignment="Center" Margin="10,0,0,0">
                    <TextBlock Text="{Binding Name, Converter={StaticResource FallbackConverter}, ConverterParameter={StaticResource NoEntryTitle}}" Style="{StaticResource ItemPrimaryPropertyStyle2}"/>
                    <TextBlock Text="{Binding Url}" Style="{StaticResource ItemSecondaryPropertyStyle}"/>
                    <TextBlock Text="{Binding UserName}" Style="{StaticResource ItemTertiaryPropertyStyle}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
        
        <DataTemplate x:Key="GroupTemplate">
            <Grid Height="110" Width="480" Margin="{StaticResource IconMargin}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="{StaticResource IconSize}" Height="{StaticResource IconSize}">
                    <TextBlock Style="{StaticResource CommonIconStyle}" Text="&#xE188;"
                               HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
                <StackPanel Grid.Column="1" VerticalAlignment="Center" Margin="10,0,0,0">
                    <TextBlock Text="{Binding Name}" Style="{StaticResource ItemPrimaryPropertyStyle}" TextWrapping="NoWrap" VerticalAlignment="Center"/>
                </StackPanel>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="NewItemTemplate">
            <Grid Height="110" Width="480" Margin="{StaticResource IconMargin}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Border Background="Transparent" Width="{StaticResource IconSize}" Height="{StaticResource IconSize}">
                    <TextBlock Style="{StaticResource CommonIconStyle}" Text="&#xE0C5;"
                               HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
                <StackPanel Grid.Column="1" VerticalAlignment="Center" Margin="10,0,0,0">
                    <TextBlock Text="{Binding Name}" Style="{StaticResource ItemPrimaryPropertyStyle}" TextWrapping="NoWrap" VerticalAlignment="Center"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
            
        <selectors:TemplateSelector x:Key="TemplateSelector">
            <selectors:TemplateSelector.Mappings>
                <selectors:TemplateSelectorMapping TypeName="SubGroupViewModel" Template="{StaticResource GroupTemplate}" />
                <selectors:TemplateSelectorMapping TypeName="EntryViewModel" Template="{StaticResource EntryTemplate}" />
                <selectors:TemplateSelectorMapping TypeName="NewItemViewModel" Template="{StaticResource NewItemTemplate}" />
            </selectors:TemplateSelector.Mappings>
        </selectors:TemplateSelector>

        <selectors:GenericStyleSelector x:Key="StyleSelector">
            <selectors:GenericStyleSelector.Mappings>
                <selectors:StyleSelectorMapping TypeName="NewItemViewModel" MappedStyle="{StaticResource GridViewAddButtonItemContainerStyle}"/>
            </selectors:GenericStyleSelector.Mappings>
        </selectors:GenericStyleSelector>
    </Page.Resources>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <!-- Back button and page title -->
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Grid.Column="1" Text="{Binding Title}" Style="{StaticResource PageHeaderTextStyle}"/>
            <TextBlock Text="{Binding Location}" Grid.Column="2" Style="{StaticResource PageSubheaderTextStyle}"/>
        </Grid>

        <GridView Grid.Row="1" ItemsSource="{Binding Items}" Padding="116,0,40,46" 
                  ItemTemplateSelector="{StaticResource TemplateSelector}" ItemClick="OnItemClick"
                  SelectionMode="Extended" IsItemClickEnabled="True"
                  SelectionChanged="OnItemSelectionChanged" ItemContainerStyleSelector="{StaticResource StyleSelector}">
            
        </GridView>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
